package com.business_clean.ui.adapter.attendance;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.uitl.GlideUtil;
import com.business_clean.data.mode.attendance.MyAttendanceDetailListEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.noober.background.drawable.DrawableCreator;

public class MyAttendanceAdapter extends BaseQuickAdapter<MyAttendanceDetailListEntity, BaseViewHolder> {

    private String classesName = "";

    public MyAttendanceAdapter() {
        super(R.layout.item_my_attendance);
        addChildClickViewIds(R.id.tv_item_classes_update, R.id.iv_item_my_attendance_action, R.id.iv_item_my_attendance_off_work_action);
    }

    public void updateClassesName(String classesName) {
        this.classesName = classesName;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, MyAttendanceDetailListEntity myAttendanceItemEntity) {
        holder.setText(R.id.tv_item_classes_name, classesName);
        //上班
        TextView attendanceDk = holder.getView(R.id.tv_item_my_attendance_dk);
        TextView tvStatus = holder.getView(R.id.tv_item_my_attendance_status);
        View lineStartView = holder.getView(R.id.view_item_start_line);
        statusTextColor(tvStatus, lineStartView, myAttendanceItemEntity.getIn_class_info().getStatus());
        ImageView imageViewDk = holder.getView(R.id.iv_item_my_attendance_action);


        if (myAttendanceItemEntity.getIn_class_info() != null) {
            holder.setText(R.id.tv_item_my_attendance_name, myAttendanceItemEntity.getIn_class_info().getTitle());
            holder.setText(R.id.tv_item_my_attendance_status, myAttendanceItemEntity.getIn_class_info().getStatus_name());

            //详情内容
            if (myAttendanceItemEntity.getIn_class_info().getClock_info() != null) {
                StringBuilder stringBuilder = new StringBuilder();
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getIn_class_info().getClock_info().getIn_class_clock_time())) {
                    stringBuilder.append("打卡时间：").append(myAttendanceItemEntity.getIn_class_info().getClock_info().getIn_class_clock_time());
                }
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getIn_class_info().getClock_info().getClock_in_type_name())) {
                    stringBuilder.append("\n").append("类型：").append(myAttendanceItemEntity.getIn_class_info().getClock_info().getClock_in_type_name());
                }
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getIn_class_info().getOperate_name())) {
                    stringBuilder.append("\n").append("操作人：").append(myAttendanceItemEntity.getIn_class_info().getOperate_name());
                }
                //增加
                attendanceDk.setText(stringBuilder.toString());
                if (stringBuilder.length() > 0) {
                    attendanceDk.setVisibility(View.VISIBLE);
                } else {
                    attendanceDk.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getIn_class_info().getClock_info().getPic_thumb())) {
                    imageViewDk.setVisibility(View.VISIBLE);
                    GlideUtil.loadCirclePic(imageViewDk.getContext(), myAttendanceItemEntity.getIn_class_info().getClock_info().getPic_thumb(),
                            imageViewDk, R.mipmap.icon_base_placeholder_working, 4);
                } else {
                    imageViewDk.setVisibility(View.GONE);
                }
            } else {
                attendanceDk.setVisibility(View.GONE);
                imageViewDk.setVisibility(View.GONE);
            }
        } else {
            attendanceDk.setText("未打卡");
        }
        //下班的内容
        TextView attendanceOffWorkDk = holder.getView(R.id.tv_item_my_attendance_off_work_dk);
        ImageView imageViewOffWorkDk = holder.getView(R.id.iv_item_my_attendance_off_work_action);

        holder.setText(R.id.tv_item_my_attendance_off_work_name, myAttendanceItemEntity.getOut_class_info().getTitle());
        holder.setText(R.id.tv_item_my_attendance_off_work_status, myAttendanceItemEntity.getOut_class_info().getStatus_name());
        TextView tvOffStatus = holder.getView(R.id.tv_item_my_attendance_off_work_status);

        View lineEndView = holder.getView(R.id.view_item_end_line);
        statusTextColor(tvOffStatus, lineEndView, myAttendanceItemEntity.getIn_class_info().getStatus());

        if (myAttendanceItemEntity.getOut_class_info() != null) {
            if (myAttendanceItemEntity.getOut_class_info().getClock_info() != null) {

                StringBuilder stringBuilder = new StringBuilder();
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getOut_class_info().getClock_info().getIn_class_clock_time())) {
                    stringBuilder.append("打卡时间：").append(myAttendanceItemEntity.getOut_class_info().getClock_info().getIn_class_clock_time());
                }
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getOut_class_info().getClock_info().getClock_in_type_name())) {
                    stringBuilder.append("\n").append("类型：").append(myAttendanceItemEntity.getOut_class_info().getClock_info().getClock_in_type_name());
                }
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getOut_class_info().getOperate_name())) {
                    stringBuilder.append("\n").append("操作人：").append(myAttendanceItemEntity.getOut_class_info().getOperate_name());
                }
                attendanceOffWorkDk.setText(stringBuilder.toString());
                if (stringBuilder.toString().length() > 0) {
                    attendanceOffWorkDk.setVisibility(View.VISIBLE);
                } else {
                    attendanceOffWorkDk.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(myAttendanceItemEntity.getOut_class_info().getClock_info().getPic_thumb())) {
                    imageViewOffWorkDk.setVisibility(View.VISIBLE);
                    GlideUtil.loadCirclePic(imageViewOffWorkDk.getContext(), myAttendanceItemEntity.getOut_class_info().getClock_info().getPic_thumb(),
                            imageViewOffWorkDk, R.mipmap.icon_base_placeholder_working, 4);
                } else {
                    imageViewOffWorkDk.setVisibility(View.GONE);
                }

            } else {
                attendanceOffWorkDk.setVisibility(View.GONE);
                imageViewOffWorkDk.setVisibility(View.GONE);
            }

        } else {
            attendanceOffWorkDk.setText("未打卡");
        }
    }

    /**
     * 设置颜色
     *
     * @param tvStatus
     * @param status
     */
    public void statusTextColor(TextView tvStatus, View view, String status) {
        if (TextUtils.isEmpty(status)) {
            return;
        }
        switch (status) {
            case "1":
                tvStatus.setTextColor(Color.parseColor("#A671CF"));
                view.setBackground(getDrawable("#A671CF"));
                break;
            case "0":
            case "2":
                tvStatus.setTextColor(Color.parseColor("#09BE89"));
                view.setBackground(getDrawable("#09BE89"));
                break;
            case "3":
                tvStatus.setTextColor(Color.parseColor("#FF801A"));
                view.setBackground(getDrawable("#FF801A"));
                break;
            case "4":
                tvStatus.setTextColor(Color.parseColor("#FF4040"));
                view.setBackground(getDrawable("#FF4040"));
                break;
            case "5":
                tvStatus.setTextColor(Color.parseColor("#000000"));
                view.setBackground(getDrawable("#000000"));
                break;
            case "6":
                tvStatus.setTextColor(Color.parseColor("#1890FF"));
                view.setBackground(getDrawable("#1890FF"));
                break;
        }
    }

    public Drawable getDrawable(String color) {
        Drawable drawable = new DrawableCreator.Builder()
                .setCornersRadius(SizeUtils.dp2px(10))
                .setSolidColor(Color.parseColor(color))
                .build();
        return drawable;
    }
}
